package com.lishe.lishe.lishe.module.me.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.base.BaseApplication;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.module.auth.view.ClearEditText2;
import com.lishe.lishe.lishe.module.auth.view.SendCodeTextView;
import com.lishe.lishe.lishe.module.me.baseAc.baseAc;
import com.lishe.lishe.lishe.module.me.http.HttpUserCenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;


/**
 * Created by zhcasecom on 2017/9/12.
 */

public class ActivityModifyPass extends baseAc implements View.OnClickListener {
    public static final String TAG = ActivityModifyPass.class.getSimpleName();

    private ClearEditText2 inputText1;
    EditText inputText2;

    private Button submitBtn;
    SendCodeTextView mInputText_sendCode;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, ActivityModifyPass.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pass);
        AuthHttpHelper.phoneOfChangePwd = null;
        BaseApplication.getInstace().addActivity(this);
        initUi();
    }

    private void initUi() {
        TextView nav_title_text = findViewById(R.id.title_text_title);
        nav_title_text.setText("修改密码");

        Button back = (Button) findViewById(R.id.title_left_button);
        back.setOnClickListener(this);

        inputText1 = findViewById(R.id.number_editTxt);
        inputText1.setText("");
        inputText1.addTextChangedListener(watcher);

        inputText2 = findViewById(R.id.code_editTxt);
        inputText2.setText("");

        submitBtn = findViewById(R.id.submit_pass_modify);
        submitBtn.setOnClickListener(this);
        submitBtn.setEnabled(false);
        submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg_disable);
        mInputText_sendCode = findViewById(R.id.sendMsgCode);
        mInputText_sendCode.setClickable(true);
        mInputText_sendCode.setOnClickListener(this);

        AuthHttpHelper.phoneOfChangePwd = null;

    }

    @Override
    protected void onResume() {
        super.onResume();
        inputText1.setText("");
        inputText2.setText("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstace().removeActivity(this);
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.title_left_button) {
            finish();
        } else if (view.getId() == R.id.redirectBtn1) {


        } else if (view.getId() == R.id.sendMsgCode) {

            //获取验证码

            AuthHttpHelper.phoneOfChangePwd = null;

            inputText2.setText("");


            final String phone = inputText1.getText().toString().trim();

            if (phone == null || phone.equals("")) {
                Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                return;
            }

            //验证手机号码
            if (!ValidationUtils.isMobileNO(phone)) {
                Toast.makeText(this, "手机号码格式有误！", Toast.LENGTH_SHORT).show();
                return;
            }
            //判断是否当前登录号码
            if (!phone.equals(SharedPreferencesUtils.getPhone(this))) {
                Toast.makeText(this, "请输入您正在登录的账号！", Toast.LENGTH_SHORT).show();
                return;
            }
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("mobile", phone);
            HttpUserCenter.doGetLoginPassCheckCode(this, params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {

                    AuthHttpHelper.phoneOfChangePwd = phone;
                    mInputText_sendCode.startCountDown();
                }

                @Override
                public void onFailure(String message) {
                    Toast.makeText(ActivityModifyPass.this, message, Toast.LENGTH_SHORT).show();
                }
            });


        } else if (view.getId() == R.id.submit_pass_modify) {

            final String phone = AuthHttpHelper.phoneOfChangePwd;
            final String checkCode = inputText2.getText().toString().trim();
            String phone_input = inputText1.getText().toString().trim();//手机号码输入框


            if (phone_input == null || phone_input.equals("") || !ValidationUtils.isMobileNO(phone_input)) {

                Toast.makeText(ActivityModifyPass.this, "手机号码格式有误!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (checkCode == null || checkCode.equals("")) {
                Toast.makeText(ActivityModifyPass.this, "验证码不能为空！", Toast.LENGTH_SHORT).show();

                return;
            }

            if (phone_input == null || !phone_input.equals(phone)) {

                Toast.makeText(ActivityModifyPass.this, "发送验证码的手机号码与当前号码不符！", Toast.LENGTH_SHORT).show();

                return;
            }

            LoadingUtils.createLoadingDialog(ActivityModifyPass.this, "请稍等...",true);

            HashMap params = new HashMap();
            params.put("mobile", phone);
            params.put("verify", checkCode);
            HttpUserCenter.doCheckRetrievedMsgVerifyCode(ActivityModifyPass.this, params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {

                    LoadingUtils.closeDialog();
                    ActivityModifyPassEnd.jump(ActivityModifyPass.this);

                }

                @Override
                public void onFailure(String message) {
                    LoadingUtils.closeDialog();
                    Toast.makeText(ActivityModifyPass.this, message, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            String input = s.toString();
            if (ValidationUtils.isMobileNO(input)) {
                submitBtn.setEnabled(true);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg);
            } else {
                submitBtn.setEnabled(false);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg_disable);
            }

        }
    };

}
